--[[
-- Author:      feilong
-- DateTime:    2018-04-23 17:19:33
-- Description: 
	模拟客户端
	1加载proto
	2连接tcp
	3登录测试
--]]

local socket = require "skynet.socket"

---------------------------------------------------------
-- class
---------------------------------------------------------
local Service = class("Robot")

function Service:ctor(name)

    self.handler = new("robot.handler")
    self:register()
end

function Service:register()
    g.dispatch:register('start', handler(self, self.onStart))
    g.dispatch:register('close', handler(self, self.onClose))

end

function Service:onStart()
    self.handler:onStart()
    --require("robot.dialog")

    g.fork(function()
        self:loop()
    end)
    return true
end

function Service:onClose()
    return true
end

--等待输入操作
function Service:loop()

    local function split_cmdline(cmdline)
        local split = {}
        for i in string.gmatch(cmdline, "%S+") do
            table.insert(split,i)
        end
        return split
    end
    local stdin = socket.stdin()
    while true do
        local cmdline = socket.readline(stdin, "\n")
        local split = split_cmdline(cmdline)
        local command = split[1]
        if not command then
            goto CONTINUE
        end
        g.log:debug("loop->", split)
        local func = self.handler[command]
        if func then
            pcall(func, select(1, table.unpack(split)))
        else
            g.log:error("handler on this func", command)
        end

        ::CONTINUE::
    end
end

return Service
